<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        class ArrayList {
            list = [10, 3, 4, 5, 6, 7, 9, 2, 8, 2, 1]

            swap(m, n) {
                let temp = this.list[m]
                this.list[m] = this.list[n]
                this.list[n] = temp
            }
            insertSort() {
                let len = this.list.length
                for (let i = 1; i < len; i++) {
                    let temp = this.list[i]
                    let j = i
                    while (this.list[j - 1] > temp && j > 0) {
                        this.list[j] = this.list[j - 1]
                        j--
                    }
                    this.list[j] = temp
                }
            }

            selectSort() {
                let len = this.list.length
                for (let i = 0; i < len - 1; i++) {
                    let min = i
                    for (let j = min + 1; j < len; j++) {
                        if (this.list[min] > this.list[j]) {
                            min = j
                        }
                    }
                    this.swap(min, i)
                }
            }

            shellSort() {
                let len = this.list.length
                let gap = Math.floor(len / 2)
                while (gap >= 1) {
                    for (let i = 0; i < len; i++) {
                        let temp = this.list[i]
                        let j = i
                        while(this.list[j-gap] > temp && j > gap-1) {
                            this.list[j] = this.list[j-gap]
                            j -= gap
                        }
                        this.list[j] = temp
                    }

                    gap = Math.floor(gap / 2)
                }

            }

            toString() {
                return this.list.join(' ')
            }


        }

        let a = new ArrayList()
        // a.insertSort()
        // a.selectSort()
        a.shellSort()
        console.log(a.toString())
    </script>
</body>

</html>